'use strict';
const {
  makeRequest,
  mixArgs,
  mixArgs_nodefault,
  add,
} = require('../src/main.js');

var assert = require('assert');
require('mocha-sinon');

describe('8_Iterators_and_Generators', function () {
  afterEach(function () {});
  beforeEach(function () {
    //this.sinon.stub(console, 'log');
    this.sinon.spy(console, 'log');
  });

  it('should Generators', function (done) {
    let gen = function* () {
      console.log('1');
      yield 1;
      console.log('2');
      yield 2;
      console.log('3');
      yield 3;
      console.log('4');
    };

    let it = gen();
    console.log('a');
    assert.strictEqual(it.next().value, 1);
    assert.strictEqual(it.next().value, 2);
    assert.strictEqual(it.next().value, 3);
    done();
  });

  it('should Delegating Generators', function (done) {
    let genNum = (function* () {
      console.log('1');
      yield 1;
      console.log('2');
      yield 2;
      console.log('3');
      yield 3;
      console.log('4');
    })();

    let gen = function* () {
      console.log('1');
      yield 1;
      console.log('2');
      yield 2;
      console.log('3');
      yield 3;
      console.log('4');
    };

    let it = gen();
    console.log('a');
    assert.strictEqual(it.next().value, 1);
    assert.strictEqual(it.next().value, 2);
    assert.strictEqual(it.next().value, 3);
    done();
  });

  it('should for...0f', async function () {
    for (let i = 0; i < 10; i++) {
      console.log(i);
    }

    console.log('step 1');

    let values = [1, 2, 3];
    for (let num of values) {
      console.log(num);
    }
    console.log('step 2');

    async function f1(){
      function asyncLog(i) {
        return new Promise((resole, reject)=>{
          setTimeout(() => {
            console.log(i);
            resole();
          }, 100);
        })
      }
      for (let num of values) {
        await asyncLog(num);
      }
      console.log('step 3');
  
      for (let num = 0; num < values.length; num++) {
        await asyncLog(num);
      }
      console.log('step 4');
    }

    f1();
    console.log('step 5');
  });
});
